/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ik.Reposteria.SMP.Ventanas.Mantenimientos;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import javax.swing.JOptionPane;
import org.ik.Reposteria.SMP.Manejadores.ManejadorPerfiles;
import org.ik.Reposteria.SMP.Ventanas.Principal;

/**
 *
 * @author Malkavian
 */
public class Permisos extends javax.swing.JFrame {

    org.ik.Reposteria.SMP.Encapsulados.Usuarios u = new org.ik.Reposteria.SMP.Encapsulados.Usuarios();
    org.ik.Reposteria.SMP.Encapsulados.Perfil usua = new org.ik.Reposteria.SMP.Encapsulados.Perfil();
    HashMap<String, org.ik.Reposteria.SMP.Encapsulados.Perfil> lista_Perfil = new HashMap<>();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    int contador = 0;
    List<org.ik.Reposteria.SMP.Encapsulados.Perfil> Perfil = null;

    private Permisos() {

        initComponents();
//
//        try {
//
//            llenarPerfil();
//
//        } catch (Exception ex) {
//            System.out.println(ex.getMessage());
//        }
//
//        texto_descripcion.setText("");
//        texto_id.setText(""+(ManejadorPerfil.getInstance().maxPerfil()+1));
        Dimension d = new Dimension(this.getSize());
        Dimension g = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(g.width / 2 - d.width / 2, g.height / 2 - d.height / 2, d.width, d.height);

        setVisible(true);
    }
    
    public Permisos(org.ik.Reposteria.SMP.Encapsulados.Usuarios u) {

        this.u = u;
        initComponents();

        try {

            llenarPerfil();

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

        texto_descripcion.setText("");
        texto_id.setText(""+(ManejadorPerfiles.getInstance().maxPerfiles()+1));
        Dimension d = new Dimension(this.getSize());
        Dimension g = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(g.width / 2 - d.width / 2, g.height / 2 - d.height / 2, d.width, d.height);

        setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton3 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        texto_id = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        texto_descripcion = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        boton_guardar = new javax.swing.JButton();
        boton_borrar = new javax.swing.JButton();
        boton_cerrar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        combo_permiso = new javax.swing.JComboBox();

        jButton3.setText("jButton3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("SMP - Informaciones Permisos");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informaciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel1.setText("Id:");

        texto_id.setEditable(false);
        texto_id.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        texto_id.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                texto_idActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel2.setText("Descripcion:");

        texto_descripcion.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(texto_id, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(texto_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(texto_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(texto_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Opciones", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        boton_guardar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_guardar.setText("Guardar");
        boton_guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_guardarActionPerformed(evt);
            }
        });

        boton_borrar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_borrar.setText("Borrar");
        boton_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_borrarActionPerformed(evt);
            }
        });

        boton_cerrar.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        boton_cerrar.setText("Cerrar");
        boton_cerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boton_cerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(boton_guardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(boton_borrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(boton_cerrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(boton_guardar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boton_borrar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(boton_cerrar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Permisos Existentes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        combo_permiso.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        combo_permiso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        combo_permiso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_permisoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(combo_permiso, 0, 201, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(combo_permiso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(7, 7, 7)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void texto_idActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_texto_idActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_texto_idActionPerformed

    private void boton_cerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_cerrarActionPerformed
        Principal p = new Principal(u);
        dispose();
    }//GEN-LAST:event_boton_cerrarActionPerformed

    private void combo_permisoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_permisoActionPerformed

        if (lista_Perfil.get("" + combo_permiso.getSelectedIndex()) != null) {

            usua = lista_Perfil.get("" + combo_permiso.getSelectedIndex());
            texto_id.setText("" + usua.getIdperfil());
            texto_descripcion.setText(usua.getDescripcion());
        }
    }//GEN-LAST:event_combo_permisoActionPerformed

    private void boton_guardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_guardarActionPerformed
        if (texto_descripcion.getText().isEmpty() || texto_id.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Favor llenar las informaciones solicitadas.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            usua.setIdperfil(Integer.parseInt(texto_id.getText()));
            usua.setDescripcion(texto_descripcion.getText());
            llenarPerfil();

        }
    }//GEN-LAST:event_boton_guardarActionPerformed

    private void boton_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boton_borrarActionPerformed

        if (texto_descripcion.getText().isEmpty() || texto_id.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "Favor seleccionar algun usuario.", "Error", JOptionPane.ERROR_MESSAGE);
        } else {

            int confirmado = JOptionPane.showConfirmDialog(null, "¿Deseas borrar este permiso?");

            if (JOptionPane.OK_OPTION == confirmado) {

                texto_descripcion.setText("");
                texto_id.setText("");
                llenarPerfil();

            }
        }

    }//GEN-LAST:event_boton_borrarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boton_borrar;
    private javax.swing.JButton boton_cerrar;
    private javax.swing.JButton boton_guardar;
    private javax.swing.JComboBox combo_permiso;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField texto_descripcion;
    private javax.swing.JTextField texto_id;
    // End of variables declaration//GEN-END:variables

    public void llenarPerfil() {
//
//        Perfil = ManejadorPerfil.getInstance().getPerfil();
//        combo_permiso.removeAllItems();
//        contador = 0;
//
//        for (org.ik.Reposteria.SMP.Encapsulados.Perfil us : Perfil) {
//
//
//            lista_Perfil.put("" + contador, us);
//            contador++;
//            combo_permiso.addItem(us.getDescripcion());
//
//        }

    }
    
}
